package com.qzdsoft.erpcloud.controller.sale;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qzdsoft.erpcloud.controller.BaseController;
import com.qzdsoft.erpcloud.domain.com.CustomerSupplierContacts;
import com.qzdsoft.erpcloud.domain.sys.LoginUser;
import com.qzdsoft.erpcloud.service.com.CustomerSupplierService;
import com.qzdsoft.erpcloud.service.com.CustomerSupplierStatementService;
import com.qzdsoft.erpcloud.service.pu.PurchaseApplyService;
import com.qzdsoft.erpcloud.service.sale.SaleQuotationService;
import com.qzdsoft.erpcloud.service.sys.Permission;
import com.qzdsoft.erpcloud.service.sys.SysLog;
import com.qzdsoft.erpcloud.service.sys.TablePermission;
import com.qzdsoft.erpcloud.vo.com.CustomerSupplierStatementInfo;
import com.qzdsoft.erpcloud.vo.sale.SaleQuotationEditEn;
import com.qzdsoft.erpcloud.vo.sale.SaleQuotationQueryCriteriaViEn;
import com.qzdsoft.erpcloud.vo.sale.SaleQuotationViEn;
import com.qzdsoft.utils.ExceptionUtil;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.ResultEnum;
import com.qzdsoft.vo.TypeInfo;
import com.qzdsoft.vo.sale.SaleOrderSourceType;
import com.qzdsoft.vo.sale.SaleQuotationStatus;

/***
 * 模块名：销售报价单
 * <p>
 * 详细描述：销售报价单Controller
 * 
 * @author DamingChen Sep 27, 2017
 * @see
 * @since 1.0
 */
@Controller
@RequestMapping("/sale/salequotation")
public class SaleQuotationController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(SaleQuotationController.class);
	private static final String BASEPATH = "sale/salequotation";

	@Autowired
	private CustomerSupplierService customerSupplierService;

	@Autowired
	private SaleQuotationService saleQuotationService;

	@Autowired
	private CustomerSupplierStatementService customerSupplierStatementService;

	/**
	 * 销售报价单首页
	 * 
	 * @return
	 */
	@TablePermission(id = "9003")
	@Permission(validate = false)
	@RequestMapping("/index")
	public ModelAndView index() {
		LoginUser user = getUser();
		ModelAndView mv = new ModelAndView();
		List<TypeInfo> status = SaleQuotationStatus.ALL;
		mv.addObject("status", status);
		mv.addObject("companys", user.getCompanys());
		mv.setViewName(BASEPATH + "/index");
		return mv;
	}

	/**
	 * 销售报价单表格列表查询
	 * 
	 * @param info
	 *            查询条件封装
	 * @return
	 */

	@SuppressWarnings("unchecked")
	// @SysLog(description="销售报价单表格列表查询")
	@ResponseBody
	@RequestMapping("/list")
	public LayTableResponse<SaleQuotationViEn> list(SaleQuotationQueryCriteriaViEn saleQuotationQueryCriteriaViEn) {
		logger.debug("查询销售报价单，参数：{}", saleQuotationQueryCriteriaViEn);
		// 如果初始化查询返回无数据空页
		if (saleQuotationQueryCriteriaViEn.isFirst()) {
			return LayTableResponse.nullDataPage();
		} else {
			return saleQuotationService.findSaleQuotationsBy(saleQuotationQueryCriteriaViEn);
		}
	}

	/**
	 * 销售报价单编辑
	 * 
	 * @param info
	 * @return
	 */
	@SysLog(description = "销售报价单编辑")
	@ResponseBody
	@RequestMapping("/edit")
	public Response<String> edit(SaleQuotationEditEn order) {
		return saleQuotationService.edit(order,getUid());
	}

	/**
	 * 销售报价单新增页面
	 * 
	 * @return
	 * @throws SecurityException 
	 * @throws NoSuchFieldException 
	 */
	@Permission(validate = false)
	@ResponseBody
	@RequestMapping("/addinit")
	public ModelAndView addinit(Integer companyId){
		String orderNo = "SQ-201709-0001";
		//获取表名(类上注解的表名值)
//	    Annotation[] classAnnotation = SaleQuotation.class.getAnnotations();
//	    Table annotation = SaleQuotation.class.getAnnotation(Table.class);  
//	    logger.info(annotation.name());  

		ModelAndView mv = new ModelAndView();
		mv.addObject("companyId", companyId);
		mv.addObject("types", SaleOrderSourceType.ALL);
		mv.addObject("orderNo", orderNo);
		mv.setViewName(BASEPATH + "/add");
		return mv;
	}

	/**
	 * 客户结算方式查询
	 * 
	 * @param supplierid
	 * @return
	 */
	@Permission(validate = false)
	@ResponseBody
	@RequestMapping("/querySettleMethod/{supplierId}")
	public List<CustomerSupplierStatementInfo> querySettleMethod(@PathVariable("supplierId") Integer supplierId) {
		// 客户结算方式
		LoginUser user = getUser();
		Integer companyId = user.getCompanyId();
		List<CustomerSupplierStatementInfo> customerSupplierStatements = customerSupplierStatementService
				.selectBy(companyId, supplierId);
		return customerSupplierStatements;
	}

	/**
	 * 客户税率信息查询
	 * 
	 * @param supplierid
	 * @return
	 */
	@Permission(validate = false)
	@ResponseBody
	@RequestMapping("/queryTax/{supplierId}/{companyId}")
	public TypeInfo queryTax(@PathVariable("supplierId") Integer supplierId,@PathVariable("companyId") Integer companyId) {
		return customerSupplierService.selectTaxInfo(companyId, supplierId);

	}

	/**
	 * 销售报价单新增提交
	 * 
	 * @param 销售报价单新增信息
	 * @return
	 */
	@SysLog(description = "新增销售报价单")
	@ResponseBody
	@RequestMapping("/add")
	public Response<String> add(SaleQuotationViEn saleQuotationViEn) {
		logger.debug("新增销售报价单:{}", saleQuotationViEn);
		try {
			saleQuotationViEn.setCreatorId(getUser().getId());
			return saleQuotationService.add(saleQuotationViEn);
		} catch (Exception ex) {
			logger.error("新增销售报价单错误，异常信息：{}", ExceptionUtil.getStackTrace(ex));
		}
		return new Response<>(ResultEnum.ERROR);
	}

	/**
	 * 修改销售报价单页面跳转
	 * 
	 * @param companyid
	 *            公司id
	 * @param id
	 *            销售报价单id
	 * @return
	 */
	@Permission(validate = false)
	@RequestMapping("/{companyId}/{id}")
	public ModelAndView detail(@PathVariable("companyId") String companyId, @PathVariable("id") Integer id) {
		logger.debug("查询销售报价单详情：companyId={},id={}", companyId, id);
		ModelAndView mv = new ModelAndView();
		SaleQuotationViEn order = saleQuotationService.findSaleQuotationBy(id);
		mv.addObject("order", order);
		List<TypeInfo> status = SaleQuotationStatus.ALL;
		mv.addObject("status", status);
		List<CustomerSupplierContacts> contacts = customerSupplierService.selectAll(order.getCustomerId() + "");
		mv.addObject("contacts", contacts);
		mv.addObject("types", SaleOrderSourceType.ALL);
		
		//结算方式
		
		mv.setViewName(BASEPATH + "/edit");
		return mv;
	}

	/**
	 * 审核销售报价单
	 * 
	 * @param id
	 *            销售报价单id
	 * @return
	 */
	@SysLog(description = "审核销售报价单")
	@ResponseBody
	@RequestMapping("/review")
	public Response<SaleQuotationViEn> review(HttpServletRequest request, Integer id) {
		Integer uid = getUid();
		return saleQuotationService.reivew(uid, id);
	}

	/**
	 * 弃审
	 * 
	 * @param id
	 * @return
	 */
	@SysLog(description = "弃审销售报价单")
	@ResponseBody
	@RequestMapping("/cancelreview")
	public Response<SaleQuotationViEn> cancelReview(HttpServletRequest request, Integer id) {
		Integer uid = getUid();
		return saleQuotationService.cancelReview(uid, id);
	}

	/**
	 * 关闭销售报价单
	 * 
	 * @param id
	 * @return
	 */
	@SysLog(description = "关闭销售报价单")
	@ResponseBody
	@RequestMapping("/close")
	public Response<String> close(Integer id) {
		return saleQuotationService.close(id);
	}

	/**
	 * 删除销售报价单
	 * 
	 * @param id
	 * @return
	 */
	@SysLog(description = "删除销售报价单")
	@ResponseBody
	@RequestMapping("/remove")
	public Response<String> remove(Integer id) {
		return saleQuotationService.remove(id);
	}
}
